
import time


import pytest
import requests
from setting import TEST_API_HOST

# 设置测试环境和清理环境
@pytest.fixture
def setup():
    # 设置测试环境
    # ...

    yield

    # 清理测试环境
    # ...


# 编写测试用例
    
def test_cabinet_status(setup):

    # cabinet_status

    url = "http://{}/cabinet_status".format(TEST_API_HOST)

    data = {"cabinet_id": 1}

    # 发送HTTP请求
    response = requests.get(url, data=data, timeout=5)

    result = response.json()

    print("cabinet_status:响应结果: {}".format(result))


    # 验证接口响应
    assert response.status_code == 200 # 正常响应
    assert result["status"] == 1 # 响应正常
    assert result["resultBody"] == "盘点结束" # 响应正常

    
    
# 正常开门功能测试
def test_open_door(setup):

    url = "http://{}/open_door".format(TEST_API_HOST)

    data = {"orderid": str(time.time()), "cabinet_id": 1}

    # 发送HTTP请求
    response = requests.post(url, data=data, timeout=5)

    result = response.json()

    print("open_door:响应结果: {}".format(result))

    # 验证接口响应
    assert response.status_code == 200 # 正常响应
    assert result["status"] == 1 # 响应正常

    # 连续发送开门期望不能正常开门
    assert requests.post(url, data=data, timeout=5).json()["errorMsg"][:9] == "设备正在执行其他任务"
    




if __name__ == '__main__':
    pytest.main([__file__])